package org.ala.tiktools.generator.mybatis.creator.query;

import org.ala.tiktools.generator.mybatis.config.info.Part;
import org.ala.tiktools.generator.mybatis.creator.entity.EntityInfo;
import org.ala.tiktools.generator.mybatis.dbinfo.TableInfo;
import org.ala.tiktools.generator.mybatis.tools.Tools;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 生成query需要的信息
 *
 * @author ala
 * @date 2024-12-31 15:22
 */
public class QueryInfo {

    /**
     *  基本entity信息
     */
    protected EntityInfo e;
    /**
     *  包名
     */
    protected String pkg;
    /**
     *  imports
     */
    protected Set<String> imports = new HashSet<>();
    /**
     *  类名
     */
    protected String className;
    /**
     *  查询字段
     */
    protected List<QueryFieldInfo> fields;

    public static QueryInfo build(TableInfo t, Part p, EntityInfo e) {
        QueryInfo qi = new QueryInfo();
        qi.e = e;
        qi.pkg = p.getOut().getQuery().getPkg();
        qi.imports.add("java.util.List");
        qi.imports.add("java.util.ArrayList");
        qi.imports.add("java.io.Serializable");
        qi.imports.add(e.getPkg() + "." + e.getClassName());
        qi.className = Tools.lowToUpFirst(Tools.hump(t.getName())) + p.getOut().getQuery().getSuffix();
        qi.fields = e.getFields().stream().map(QueryFieldInfo::build).filter(QueryFieldInfo::isQuery).collect(Collectors.toList());
        return qi;
    }

    public EntityInfo getE() {
        return e;
    }

    public void setE(EntityInfo e) {
        this.e = e;
    }

    public List<QueryFieldInfo> getFields() {
        return fields;
    }

    public void setFields(List<QueryFieldInfo> fields) {
        this.fields = fields;
    }

    public String getPkg() {
        return pkg;
    }

    public void setPkg(String pkg) {
        this.pkg = pkg;
    }

    public Set<String> getImports() {
        return imports;
    }

    public void setImports(Set<String> imports) {
        this.imports = imports;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}
